package cn.edu.scau.cmi.huangmaoqin.hibernate.dao;

import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Transaction;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Student;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Teacher;

public class StudentDAO extends BaseHibernateDAO {

	public void save(Student student) {
		try {
			Transaction transaction = getSession().beginTransaction();
			getSession().save(student);
			// 提交事务
			transaction.commit();
			Teacher teacher = student.getTeacher();
			if (teacher != null) {
				teacher.getStudents().add(student);
				new TeacherDAO().update(teacher);
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Student student) {
		try {
			Teacher teacher = student.getTeacher();
			// 老师断掉和学生的关系
			if (teacher != null) {
				Set<Student> stus = teacher.getStudents();
				for (Student student2 : stus) {
					if (student2.getId().equals(student.getId())) {
						stus.remove(student2);
						break;
					}
				}
				teacher.setStudents(stus);
				new TeacherDAO().update(teacher);
			}
			Transaction transaction = getSession().beginTransaction();
			getSession().delete(student);
			transaction.commit();

		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void update(Student student, Teacher oldT) {
		try {
			Transaction transaction = getSession().beginTransaction();
			getSession().update(student);
			transaction.commit();

			// 删除旧老师的联系
			if (oldT != null) {
				Set<Student> stus = oldT.getStudents();
				for (Student student2 : stus) {
					if (student2.getId().equals(student.getId())) {
						stus.remove(student2);
						break;
					}
				}
				oldT.setStudents(stus);
				new TeacherDAO().update(oldT);
			}
			// 建立新老师的联系
			Teacher newT = student.getTeacher();
			if (newT != null) {
				newT.getStudents().add(student);
				new TeacherDAO().update(newT);
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 只增加或减少了一个学生
	 * 
	 * @param student
	 */
	public void update(Student student) {
		try {
			Transaction transaction = getSession().beginTransaction();
			getSession().update(student);
			transaction.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Student findById(java.lang.Long id) {
		try {
			Student instance = (Student) getSession().get("cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Student", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findAll() {
		try {
			String queryString = "from Student";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}